'use strict';


/**
 * List of daily
 */
exports.search = function(req, res) {
    var data = {};
    data=req.body;
    var sql="";
    if(JSON.stringify(data) != "{}"){
        sql="where ";
        var lastObj="";
        for (var prop in data) {
            data['daily.'+prop]=data[prop];
            delete data[prop];
        }
        for (var prop in data){
            lastObj=prop;
        }
        for (var prop in data)
        {
            sql+=" "+prop+"='"+data[prop]+"' ";
            if(prop!=lastObj){
                sql+=" AND ";
            }
        }
        // sql+=" AND ";
    }
    // console.log("SELECT daily.*,users.name FROM daily LEFT JOIN users on users.id=daily.user_id  "+sql);
    req.getConnection(function(err, conn) {
        if (err) {
            return res.send(401);
        } else {
            conn.query("SELECT daily.*,users.name FROM daily LEFT JOIN users on users.id=daily.user_id  "+sql, function(err,result) {
                if (err) {
                    return next(err);
                } else {
                    res.json(result); 
                }
            });
        }
    });
};

/**
 * add daily
 */
exports.add = function(req, res) {
    var data = {};
    data=req.body;
    if(JSON.stringify(data) != "{}"){
        var sql="(";
        var lastObj="";
        for (var prop in data){
            lastObj=prop;
        }
        for (var prop in data)
        {
            sql+=prop;
            if(prop!=lastObj){
                sql+=" , ";
            }else{
                sql+=") values (";
            }
        }
        for (var prop in data)
        {
            sql+="'"+data[prop]+"' ";
            if(prop!=lastObj){
                sql+=" , ";
            }else{
                sql+=") ";
            }
        }
        req.getConnection(function(err, conn) {
            if (err) {
                return res.send(401);
            } else {
                conn.query('insert into daily '+sql, function(err,result) {
                    if (err) {
                        res.json(false);
                    } else {
                        res.json(true); 
                    }
                });
            }
        });
    }else{
        res.json(false);
    }
};

/**
 * set sales
 */
exports.set = function(req, res) {
    var data = {};
    data=req.body;
    var sql="";
    if(data!={}){
        var lastObj="";
        for (var prop in data){
            if(prop!="id"){
                lastObj=prop;
            }
        }
        for (var prop in data)
        {
            if(prop!="id"){
                sql+=" "+prop+"='"+data[prop]+"' ";
                if(prop!=lastObj){
                    sql+=" , ";
                }
            }
        }
        sql+="where id="+data['id'];
    }
    req.getConnection(function(err, conn) {
        if (err) {
            return res.send(401);
        } else {
            conn.query('UPDATE daily SET '+sql, function(err,result) {
                if (err) {
                    res.json(false);
                } else {
                    res.json(true);
                }
            });
        }
    });
};